//
//  GroupParentCheckViewController.m
//  Qihuo
//
//  Created by Vencoo on 14-1-2.
//  Copyright (c) 2014年 Vencoo. All rights reserved.
//

#import "GroupParentCheckViewController.h"
#import "ParentCheckCell.h"

@interface GroupParentCheckViewController ()

@end

@implementation GroupParentCheckViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self showMainView];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSURL *queryUrl = [NSURL URLWithString:_checkURL];
    ASIFormDataRequest *obtainFloor = [ASIFormDataRequest requestWithURL:queryUrl];
    [obtainFloor setPostValue:self.delegate.sendCheckTime forKey:@"checkTime"];
    [obtainFloor startAsynchronous];
    obtainFloor.delegate = self;
    [obtainFloor setDidFinishSelector:@selector(requestDone:)];
    [obtainFloor setDidFailSelector:@selector(requestFail)];
    _floorCheckDatas = [NSMutableArray array];    
}
- (void)showMainView
{
    UIImage *lineImage = [UIImage imageNamed:@"黑边框"];
    UIImage *whiteimage = [UIImage imageNamed:@"底框-白.png"];
    UIImageView *MyView = [[UIImageView alloc]initWithFrame:CGRectMake((scrennWidth - whiteimage.size.width)/2, 90, whiteimage.size.width, whiteimage.size.height + 80)];
    MyView.image = whiteimage;
    [MyView setUserInteractionEnabled:YES];
    [self.view addSubview:MyView];
    
    UIImageView *lineView = [[UIImageView alloc]initWithImage:lineImage];
    lineView.frame = CGRectMake((whiteimage.size.width - lineImage.size.width)/2, 40 + (whiteimage.size.height - lineImage.size.height)/2, lineImage.size.width, lineImage.size.height + 40);
    lineView.userInteractionEnabled = YES;
    [MyView addSubview:lineView];
    
    _floorLb = [UILabel new];
    _floorLb.text = @"楼层/位置";
    _floorLb.font = [UIFont systemFontOfSize:30];
    CGSize mysz = [_floorLb.text sizeWithFont:_floorLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    _floorLb.frame = CGRectMake(lineView.frame.origin.x + 50, (lineView.frame.origin.y - mysz.height)/2, mysz.width, mysz.height);
    [MyView addSubview:_floorLb];
    
    for (int i = 0; i < _checkNames.count; i ++)
    {
        UILabel *floorLbNew = [UILabel new];
        floorLbNew.text = [_checkNames objectAtIndex:i];
        floorLbNew.font = [UIFont systemFontOfSize:20];
        mysz = [[NSString stringWithFormat:@"啊啊啊啊"] sizeWithFont:floorLbNew.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        floorLbNew.numberOfLines = 0;
        floorLbNew.textAlignment = 1;
        if (floorLbNew.text.length > 4)
        {
            floorLbNew.frame = CGRectMake((MyView.frame.size.width - 30 - _floorLb.frame.size.width)/_checkNames.count*i+((lineView.frame.size.width - 30 - _floorLb.frame.size.width)/_checkNames.count-mysz.width)/2 + _floorLb.frame.origin.x + _floorLb.frame.size.width, _floorLb.frame.origin.y,mysz.width, mysz.height*2);
        }
        else
        {
            floorLbNew.frame = CGRectMake((MyView.frame.size.width - 30 - _floorLb.frame.size.width)/_checkNames.count*i+((lineView.frame.size.width - 30 - _floorLb.frame.size.width)/_checkNames.count-mysz.width)/2 + _floorLb.frame.origin.x + _floorLb.frame.size.width, _floorLb.frame.origin.y + mysz.height/2,(MyView.frame.size.width - 30 - _floorLb.frame.size.width)/_checkNames.count, mysz.height*(1 + mysz.width/(MyView.frame.size.width - 30 - _floorLb.frame.size.width)/_checkNames.count));
        }
        [MyView addSubview:floorLbNew];
    }
    
    _mainTable = [[UITableView alloc]initWithFrame:CGRectMake(2, 2, lineImage.size.width-6, lineImage.size.height+20) style:UITableViewStylePlain];
    _mainTable.dataSource = self;
    _mainTable.delegate = self;
    [lineView addSubview:_mainTable];
    _mainTable.tag = 100;
}
#pragma mark -
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.floorCheckDatas objectAtIndex:section]count];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _floorNames.count;
}
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    return [self.floorNames objectAtIndex:section];
//}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:index];
    UILabel *label = [UILabel new];
    label.text = [self.floorNames objectAtIndex:section];
    label.font = [UIFont systemFontOfSize:20];
    CGSize cgSz = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    label.frame = CGRectMake((cell.frame.size.width - cgSz.width)/2, (cell.frame.size.height - cgSz.height)/2, cell.frame.size.width, cell.frame.size.height);
    label.textAlignment = 1;
    label.backgroundColor = [UIColor lightGrayColor];
    return label;
}
#pragma mark - request
- (void)requestDone:(ASIFormDataRequest *)request
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    NSString *response = [request responseString];
    NSDictionary *queryDic = [response JSONValue];
    NSDictionary *queryDataArr = [queryDic objectForKey:@"data"];
    NSLog( @"%@",queryDataArr);
    NSArray *floorData = [queryDataArr objectForKey:@"floorData"];
    
    if ([[floorData objectAtIndex:0] valueForKey:@"floorName"])
    {
        _floorNames = [NSMutableArray array];
    }
    if ([[floorData objectAtIndex:0] valueForKey:@"floorRemark"])
    {
        _floorRemarks = [NSMutableArray array];
    }
    for (int i = 0; i < floorData.count; i ++)
    {
        NSDictionary *queryDic = [floorData objectAtIndex:i];
        if ([queryDic objectForKey:@"floorName"]) {
            [_floorNames addObject:[queryDic valueForKey:@"floorName"]];
        }
        if ([queryDic objectForKey:@"floorRemark"]) {
            [_floorRemarks addObject:[queryDic valueForKey:@"floorRemark"]];
        }
        if ([queryDic objectForKey:@"checkData"]) {
            [_floorCheckDatas addObject:[queryDic valueForKey:@"checkData"]];
        }
    }
    if ([[_floorCheckDatas objectAtIndex:0]valueForKey:@"process"])
    {
        _chekckProcesses = [NSMutableArray array];
    }
    if ([[_floorCheckDatas objectAtIndex:0]valueForKey:@"result"])
    {
        _checkResults = [NSMutableArray array];
    }
    if ([[_floorCheckDatas firstObject]valueForKey:@"checkName"])
    {
        _checkMyNames = [NSMutableArray array];
    }
    if ([[_floorCheckDatas firstObject]valueForKey:@"childData"])
    {
        _childDatas = [NSMutableArray array];
    }
    for (int i = 0; i < self.floorCheckDatas.count; i ++)
    {
        NSArray *floorDatas = [self.floorCheckDatas objectAtIndex:i];
        NSMutableArray *process = [NSMutableArray array];
        NSMutableArray *results = [NSMutableArray array];
        NSMutableArray *checkName = [NSMutableArray array];
        NSMutableArray *chidData = [NSMutableArray array];
        for (int j = 0; j < floorDatas.count; j ++)
        {
            NSDictionary *floorDic = [floorDatas objectAtIndex:j];
            if ([floorDic objectForKey:@"process"]) {
                [process addObject:[floorDic objectForKey:@"process"]];
            }
            if ([floorDic objectForKey:@"result"]) {
                [results addObject:[floorDic objectForKey:@"result"]];
            }
            if ([floorDic objectForKey:@"checkName"]) {
                [checkName addObject:[floorDic objectForKey:@"checkName"]];
            }
            if ([floorDic objectForKey:@"childData"]) {
                [chidData addObject:[floorDic objectForKey:@"childData"]];
            }
        }
        [_checkMyNames addObject:checkName];
        [_chekckProcesses addObject:process];
        [_checkResults addObject:results];
        [_childDatas addObject:chidData];
    }
    [_mainTable reloadData];
}
- (void)requestFail
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    UIAlertView *failAlert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"请求数据失败！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [failAlert show];
}
#pragma mark - end
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
